package com.sgb.contract.view.ui.openaccount;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import com.google.gson.Gson;
import com.sgb.contract.R;
import com.sgb.contract.base.BaseActivity;
import com.sgb.contract.callback.ITextWatcher;
import com.sgb.contract.databinding.ContractAlterinfoActivityBinding;
import com.sgb.contract.test.model.OpenAccountDetailEntity;
import com.swgk.core.dialog.RoundDialog;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * 作者:张磊
 * 日期:2022/1/4 0004
 * 说明:修改信息
 */
public class AlterInfoActivity extends BaseActivity<ContractAlterinfoActivityBinding, OpenAccountViewModel> {


    private OpenAccountDetailEntity mData;

    public static void start(Context context, String phone, String data) {
        Intent intent = new Intent(context, AlterInfoActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("phone", phone);
        intent.putExtra("data", data);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.contract_alterinfo_activity;
    }

    @Override
    protected void init() {
        mDataBinding.head.title.setText("修改账号信息");
        mDataBinding.tvPhoneNumber.setText(getIntent().getStringExtra("phone"));
        mData = new Gson().fromJson(getIntent().getStringExtra("data"), OpenAccountDetailEntity.class);
    }


    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(OpenAccountViewModel.class, 0);
    }

    @Override
    protected void initListener() {
        mDataBinding.tvGetCode.setOnClickListener(v -> {
            if (TextUtils.isEmpty(mDataBinding.edPhone.getText().toString())) {
                mDataBinding.txtTipPhone.setText("请输入手机号码");
                mDataBinding.txtTipPhone.setVisibility(View.VISIBLE);
                return;
            }
            startTimer();
            mViewModel.getValidateCode(this, mDataBinding.edPhone.getText().toString());
        });


        mDataBinding.imgTip.setOnClickListener(v -> {
            new RoundDialog.Builder(this)
                    .title("温馨提示")
                    .content("用于接收电子签章验证码的手机号")
                    .confirm("知道了")
                    .singleBtn(true)
                    .contentGravity(Gravity.CENTER)
                    .cancelable(true)
                    .show();
        });
        mDataBinding.etVerificationCode.addTextChangedListener(new ITextWatcher() {


            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(mDataBinding.etVerificationCode.getText().toString())) {
                    mDataBinding.txtTipCode.setVisibility(View.GONE);
                }
            }

        });
        mDataBinding.edPhone.addTextChangedListener(new ITextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(mDataBinding.edPhone.getText().toString())) {
                    mDataBinding.txtTipPhone.setVisibility(View.GONE);
                }
            }
        });
        mDataBinding.tvBtn.setOnClickListener(v -> {
            if (TextUtils.isEmpty(mDataBinding.edPhone.getText().toString())) {
                mDataBinding.txtTipPhone.setText("请输入手机号码");
                mDataBinding.txtTipPhone.setVisibility(View.VISIBLE);
                return;
            }
            if (!isChinaPhoneLegal(mDataBinding.edPhone.getText().toString())) {
                mDataBinding.txtTipPhone.setText("手机号码格式错误");
                mDataBinding.txtTipPhone.setVisibility(View.VISIBLE);
                return;
            }
            if (TextUtils.isEmpty(mDataBinding.etVerificationCode.getText().toString())) {
                showTipCodeTxt("请输入验证码");
                return;
            }
            Map map = new HashMap();
            map.put("id", mData.id);
            map.put("identityCard", mData.identityCard);
            map.put("name", mData.name);
            map.put("phone", mDataBinding.edPhone.getText().toString());
            map.put("validateCode", mDataBinding.etVerificationCode.getText().toString());
            mViewModel.updatePersonAccount(this, map);
        });
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
    }


    public static boolean isChinaPhoneLegal(String phone) throws PatternSyntaxException {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[0-9]))\\d{8}$";
        if (phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            return isMatch;
        }
    }

    public void showTipCodeTxt(String tx) {
        mDataBinding.txtTipCode.setText(tx);
        mDataBinding.txtTipCode.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onDialogOnClick(View dialogView) {
        if ("ok".equals(dialogView.getTag().toString())) {
            finish();
        }
    }


    private void startTimer() {
        CountDownTimer timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mDataBinding.tvGetCode.setEnabled(false);
                String str = String.format("重新获取(%1$d)", millisUntilFinished / 1000);
                mDataBinding.tvGetCode.setText(str);
                mDataBinding.tvGetCode.setTextColor(getResources().getColor(R.color.color_C0C4CC));
            }

            @Override
            public void onFinish() {
                mDataBinding.tvGetCode.setEnabled(true);
                mDataBinding.tvGetCode.setText("获取验证码");
                mDataBinding.tvGetCode.setTextColor(getResources().getColor(R.color.color_ffb79255));
            }
        };
        timer.start();
    }

}